package com.dd.ljzapplication.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.dd.ljzapplication.R;
import com.emiage.ddutils.BitmapUtils;
import com.emiage.ddutils.UIUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by LiJZ on 2016/9/22.
 * 手写签字
 *	注意：画布的大小，既手写板的大小，要和显示画布的容器的大小，既ImageViwe的大小一致，否则，触摸动作会有偏移
 */
public class DrawActivity extends Activity {

	private static final String TAG = "####";
	private ImageView mImageView;
	private Bitmap bitmap;
	private LinearLayout linearLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		mImageView = (ImageView) findViewById(R.id.iv);
		linearLayout = (LinearLayout) findViewById(R.id.ll_content);

		int height = linearLayout.getLayoutParams().height;

		int screenHeight = getScreenHeight(this);
		int screenWidth = getScreenWidth(this);
		Log.i(TAG, "screenWidth:" + screenWidth + "-------screenHeight:" + screenHeight);
		int i = UIUtils.dip2px(40);
		bitmap = Bitmap.createBitmap(screenWidth, UIUtils.dip2px(250), Bitmap.Config.ARGB_8888);
//		bitmap = Bitmap.createBitmap(screenWidth, screenHeight * 5 / 4, Bitmap.Config.ARGB_8888);
		final Canvas canvas = new Canvas(bitmap);
		final Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(dip2px(2));
		canvas.drawColor(getResources().getColor(R.color.cornsilk));
//		canvas.drawColor(Color.WHITE);
		mImageView.setImageBitmap(bitmap);
		mImageView.setOnTouchListener(new View.OnTouchListener() {

			private float y;
			private float x;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						x = event.getX();
						y = event.getY();
						Log.i(TAG, "getX: " + x + "-------getY:" + y);
						break;
					case MotionEvent.ACTION_MOVE:
						float x1 = event.getX();
						float y1 = event.getY();
						Log.i(TAG, "ACTION_MOVE:------getX: " + x1 + "-------getY:" + y1);
						canvas.drawLine(x, y, x1, y1, paint);
						mImageView.setImageBitmap(bitmap);
						x = x1;
						y = y1;
						break;
					case MotionEvent.ACTION_UP:
						break;
					default:
						break;
				}
				return true;
			}
		});
	}

	public void save(View view) {
		File file = new File(Environment.getExternalStorageDirectory().getPath() + "/.aaaaaaaaljz/IMG_" + System.currentTimeMillis() + ".png");
		try {
			FileOutputStream fos = new FileOutputStream(file);
//			bitmap.compress(Bitmap.CompressFormat.JPEG, 1, fos);
			Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 960, 375, false);
			String s = BitmapUtils.bitmapToString(scaledBitmap, 50);
			int length = s.length();
			scaledBitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);
			fos.flush();
			fos.close();
//			Intent intent = new Intent();
//			intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
//			intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
//			sendBroadcast(intent);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

			Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获得屏幕高度
	 *
	 * @param context
	 * @return
	 */
	private int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获得屏幕宽度
	 *
	 * @param context
	 * @return
	 */
	private int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	/**
	 * dip转换px
	 */
	private int dip2px(int dip) {
		final float scale = this.getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}

	public void video(View view) {
//		Intent intent = new Intent(this, ThirdActivity.class);
//		startActivity(intent);
	}
}
